package com.clj.cloud.authorization_server.oauth2;

import java.util.Set;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.clj.cloud.authorization_server.entity.Role;
import com.clj.cloud.authorization_server.entity.User;
import com.clj.cloud.authorization_server.service.IRoleService;
import com.clj.cloud.authorization_server.service.IUserService;
@Slf4j
@Service("userDetailsService")
public class CustomUserDetailsService implements UserDetailsService {
	
	@Autowired
	private IUserService userService;
	
	@Autowired
    private IRoleService roleService;
	
	
	
	@Override
	public UserDetails loadUserByUsername(String uniqueId)
			throws UsernameNotFoundException {
		User user = userService.getByUniqueId(uniqueId);
        log.info("load user by username :{}", user.toString());
		return new org.springframework.security.core.userdetails.User(
                user.getUsername(),
                user.getPassword(),
                user.getEnabled(),
                user.getAccountNonExpired(),
                user.getCredentialsNonExpired(),
                user.getAccountNonLocked(),
                this.obtainGrantedAuthorities(user));
	}
	
	/**
     * 获得登录者所有角色的权限集合.
     *
     * @param user
     * @return
     */
    protected Set<GrantedAuthority> obtainGrantedAuthorities(User user) {
        Set<Role> roles = roleService.queryUserRolesByUserId(user.getId());
        log.info("user:{},roles:{}", user.getUsername(), roles);
        return roles.stream().map(role -> new SimpleGrantedAuthority(role.getCode())).collect(Collectors.toSet());
    }

}
